package com.muyu.danmo.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.common.system.domain.LoginUser;
import com.muyu.danmo.domain.DanmoAddresses;
import com.muyu.danmo.service.IDanmoAddressesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 用户收货地址Controller
 *
 * @author yangle
 * @date 2024-10-05
 */
@RestController
@RequestMapping("/addresses")
public class DanmoAddressesController extends BaseController
{
    @Autowired
    private IDanmoAddressesService danmoAddressesService;

    /**
     * 查询用户收货地址列表
     */
//    @RequiresPermissions("system:addresses:list")
    @PostMapping("/list")
    public Result list(@RequestBody DanmoAddresses danmoAddresses)
    {
        PageHelper.startPage(danmoAddresses.getPageNum(),danmoAddresses.getPageSize());
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser==null){
            return Result.notLoggedIn("请登录");
        }
        danmoAddresses.setUserId(loginUser.getUserid());
        List<DanmoAddresses> list = danmoAddressesService.selectDanmoAddressesList(danmoAddresses);
        PageInfo<DanmoAddresses> danmoInformationPageInfo = new PageInfo<>(list);
        return Result.success(danmoInformationPageInfo);
    }

    /**
     * 导出用户收货地址列表
     */
//    @RequiresPermissions("system:addresses:export")
    @Log(title = "用户收货地址", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DanmoAddresses danmoAddresses)
    {
        List<DanmoAddresses> list = danmoAddressesService.selectDanmoAddressesList(danmoAddresses);
        ExcelUtil<DanmoAddresses> util = new ExcelUtil<DanmoAddresses>(DanmoAddresses.class);
        util.exportExcel(response, list, "用户收货地址数据");
    }

    /**
     * 获取用户收货地址详细信息
     */
//    @RequiresPermissions("system:addresses:query")
    @GetMapping(value = "/{id}")
    public Result getInfo(@PathVariable("id") Long id)
    {
        return Result.success(danmoAddressesService.selectDanmoAddressesById(id));
    }

    /**
     * 新增用户收货地址
     */
//    @RequiresPermissions("system:addresses:add")
    @Log(title = "用户收货地址", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public Result add(@RequestBody DanmoAddresses danmoAddresses)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser==null){
            return Result.notLoggedIn("请登录");
        }
        danmoAddresses.setUserId(loginUser.getUserid());
        return     danmoAddressesService.insertDanmoAddresses(danmoAddresses);

    }

    /**
     * 修改用户收货地址
     */
//    @RequiresPermissions("system:addresses:edit")
    @Log(title = "用户收货地址", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public Result edit(@RequestBody DanmoAddresses danmoAddresses)
    {
        String token = SecurityUtils.getToken();
        System.out.println("token"+token);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser==null){
            return Result.notLoggedIn("请登录");
        }
        danmoAddresses.setUserId(loginUser.getUserid());
        return danmoAddressesService.updateDanmoAddresses(danmoAddresses);
    }

    /**
     * 删除用户收货地址
     */
//    @RequiresPermissions("system:addresses:remove")
    @Log(title = "用户收货地址", businessType = BusinessType.DELETE)
    @PostMapping("del/{ids}")
    public Result remove(@PathVariable Long[] ids)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser==null){
            return Result.notLoggedIn("请登录");
        }
        return danmoAddressesService.deleteDanmoAddressesByIds(ids);
    }
}
